import { MessageType, MessageItem } from '@/views/Message/type'
import { defineStore } from 'pinia'

interface IMessageState {
  noticeList: MessageItem[]
  orderList: MessageItem[]
}

const useMessageStore = defineStore('message', {
  state: (): IMessageState => ({
    noticeList: [
      {
        key: crypto.randomUUID(),
        title: '活动通知',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: crypto.randomUUID(),
        title: '免费得iPhone11',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: crypto.randomUUID(),
        title: '这里的字符限制是十二个字符',
        content: '恭喜获取20租币，请查收',
        date: '2023-04-01 14:30',
        status: 0
      },
      {
        key: crypto.randomUUID(),
        title: '节日赠租金',
        content: '人人租祝您节日快乐，送88个租币人人租祝您节日快乐，送88个租币！！！',
        date: '2023-04-01 14:30',
        status: 0
      }
    ],
    orderList: [
      {
        key: crypto.randomUUID(),
        title: '订单已取消',
        content: '订单已成功取消，感谢您的关注',
        date: '2023-04-01 14:30',
        status: 0
      }
    ]
  }),
  actions: {
    getList(type: MessageType, num: number) {
      return new Promise((resolve) => {
        const template: Record<MessageType, MessageItem> = {
          notice: {
            key: crypto.randomUUID(),
            title: '活动通知',
            content: '恭喜获取20租币，请查收',
            date: '2023-04-01 14:30',
            status: 0
          },
          order: {
            key: crypto.randomUUID(),
            title: '订单已取消',
            content: '订单已成功取消，感谢您的关注',
            date: '2023-04-01 14:30',
            status: 0
          }
        }
        const item = template[type]

        setTimeout(() => {
          for (let i = 0; i < num; i++) {
            this[`${type}List`].push({ ...item, key: crypto.randomUUID() })
          }
          resolve(this[`${type}List`])
        }, 1000)
      })
    },
    removeState(type: MessageType, key: string) {
      this[`${type}List`] = this[`${type}List`].filter((item) => item.key !== key)
    },
    updateStatus(type: MessageType, key: string) {
      this[`${type}List`].forEach((item) => {
        if (item.key === key) item.status = 1
      })
    }
  }
})

export default useMessageStore
